from game_plug_in.function.object_detction import load_model
from game_plug_in.function.object_detction import interface_img
from game_plug_in.function.grab_screen import grab_screen_win32_v2
from game_plug_in.function.grab_screen import update_hwnd_title


from game_plug_in.LogitechDriver.logitech import Logitech

from pynput import mouse
from pynput import keyboard
import time
import cv2
import numpy as np

grab_window_title='Counter-Strike: Global Offensive - Direct3D 9'
screen_width,screen_height=1280,720
grab_width,grab_height = 1280,720
grab_rectangle=(0,0,grab_width,grab_height)

is_mouse_left = False
is_mouse_right = False
mouse_aim = (10, 5)
mouse_offset_ratio = 3  #偏移系数
flag_lock_obj = True

target_lock = 0
lock_model = 0
flag_lock_t = False
flag_lock_t_head = False
flag_lock_ct = False
flag_lock_ct_head = False

shot_interval_time = 0.1


def on_press(key):
    global flag_lock_obj,target_lock,lock_model
    global flag_lock_t,flag_lock_t_head,flag_lock_ct,flag_lock_ct_head
    if key == keyboard.Key.home:
        lock_model += 1
        if lock_model>=6:
            lock_model=0

    print(lock_model)

    if lock_model==0:
        flag_lock_obj = 0
        print(f'锁定功能关闭')
    elif lock_model==1:
        flag_lock_obj = 1
        flag_lock_t = True
        print(f'目前为CT，开启T锁定')
    elif lock_model==2:
        flag_lock_t = False
        flag_lock_t_head = True
        print(f'目前为CT，开启T_head锁定')
    elif lock_model==3:
        flag_lock_t_head = False
        flag_lock_ct = True
        print(f'目前为T，开启CT锁定')
    elif lock_model==4:
        flag_lock_ct = False
        flag_lock_ct_head = True
        print(f'目前为T，开启CT_head锁定')
    elif lock_model==5:
        flag_lock_ct_head = False
        flag_lock_obj = 1
    print(key)


def on_click(x, y, button, pressed):
    global is_mouse_left, is_mouse_right, mouse_aim,target_lock
    if pressed:
        if button == mouse.Button.left:
            is_mouse_left = True
            #target_lock = 1
        elif button == mouse.Button.right:
            is_mouse_right = True
        elif button == mouse.Button.x1:
            #mouse_aim = (mouse_aim[0] + 100, mouse_aim[1])
            target_lock=1
            print(f'mouse_aim:{mouse_aim}')
        elif button == mouse.Button.x2:
            #mouse_aim = (mouse_aim[0] - 100, mouse_aim[1])
            #
            print(f'mouse_aim:{mouse_aim}')
    else:
        if button == mouse.Button.left:
            is_mouse_left = False
            #target_lock = 0
        elif button == mouse.Button.right:
            is_mouse_right = False
        elif button == mouse.Button.x1:
            target_lock=0
    print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
    print(f'is_mouse_left:{is_mouse_left}  is_mouse_right:{is_mouse_right}')
    print(button)



def send_nearest_pos_to_mouse_ctrl(box_list,mark_listtype):
    global flag_lock_t, flag_lock_t_head, flag_lock_ct, flag_lock_ct_head
    if len(box_list) == 0:
        return  None
    dis_min=(grab_width/2)**2+(grab_height/2)**2
    pos_min=(0,0)
    for _box in box_list:
        x_center=_box[1]*grab_width
        if mark_listtype == 1:
            y_center=_box[2]*grab_height-_box[4] * grab_height/2
        elif mark_listtype == 2:
            y_center = _box[2] * grab_height
        if (x_center-grab_width/2)**2+(y_center-grab_height/2)**2<dis_min:
            dis_min=(x_center-grab_width/2)**2+(y_center-grab_height/2)**2
            pos_min=(x_center,y_center)

    if pos_min == (0,0):
        return None

    #Logitech.mouse.move(int(pos_min[0]*mouse_offset_ratio), int(pos_min[1]*mouse_offset_ratio))
    Logitech.mouse.move(int((pos_min[0]-grab_width/2) * mouse_offset_ratio), int((pos_min[1]-grab_height/2) * mouse_offset_ratio))


def draw_box(img,box_list):
    if len(box_list) == 0:
        return img
    for _box in box_list:
        x_center = _box[1] * grab_width
        y_center = _box[2] * grab_height
        w = _box[3] * grab_width
        h = _box[4] * grab_height
        x1, y1 = int(x_center - w / 2), int(y_center - h / 2)
        x2, y2 = int(x_center + w / 2), int(y_center + h / 2)
        cv2.rectangle(img,(x1,y1),(x2,y2),color=(0,255,0),thickness=4)
        cv2.putText(img,f'{_box[0]}_{_box[5]}%',(x1,y1),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,0,255),2)

    return img

def draw_fps(img,fps_tag,fps_list):
    timer=time.time()-fps_tag
    if len(fps_list)>10:
        fps_list.pop(0)
        fps_list.append(timer)
    else:
        fps_list.append(timer)

    cv2.putText(img,str(1/np.mean(fps_list)),(20,40),cv2.FONT_HERSHEY_COMPLEX,0.8,(0,0,255),2)
    return img

def main():
    listener_mouse = mouse.Listener(on_click=on_click)
    listener_mouse.start()
    listener_keyboard = keyboard.Listener(on_press=on_press)
    listener_keyboard.start()

    fps_list = []  #记录每帧运行的时间

    model = load_model()

    while True:
        fps_tag = time.time()

        img=grab_screen_win32_v2(window_title=grab_window_title,grab_rect=grab_rectangle)

        box_list = interface_img(img,model)
        #print(box_list)

        #if is_mouse_right and is_mouse_left and flag_lock_obj:
        #if target_lock:

        if target_lock and flag_lock_obj:
            listtype = 0
            box_list_select = []
            if flag_lock_ct:
                listtype=1
                for _box in box_list:
                    if _box[0] == 'CT':
                        box_list_select.append(_box)
            elif flag_lock_ct_head:
                listtype=2
                for _box in box_list:
                    if _box[0] == 'CT_head':
                        box_list_select.append(_box)
                if len(box_list_select):
                    listtype=1
                    for _box in box_list:
                        if _box[0] == 'CT':
                            box_list_select.append(_box)

            elif flag_lock_t:
                listtype=1
                for _box in box_list:
                    if _box[0] == 'T':
                        box_list_select.append(_box)
            elif flag_lock_t_head:
                listtype=2
                for _box in box_list:
                    if _box[0] == 'T_head':
                        box_list_select.append(_box)
                if len(box_list_select):
                    listtype=1
                    for _box in box_list:
                        if _box[0] == 'T':
                            box_list_select.append(_box)
            else:
                listtype = 1
                box_list_select=box_list

            send_nearest_pos_to_mouse_ctrl(box_list_select,listtype)

        #print(box_list)
        img=draw_box(img,box_list)

        img=draw_fps(img,fps_tag,fps_list)

        cv2.imshow('game plug in',img)
        cv2.waitKey(1)



if __name__ == '__main__':
    update_hwnd_title()
    main()










